<template>
  <div style="width: 100%">
    <avue-crud :option="{...option,column,menu:false,header:!noBtn}"
               :table-loading="loading"
               :data="tableData"
               ref="crud">
      <template slot="menuLeft" v-if="!noBtn">
        <el-button size="small" type="primary" :disabled="disabled" @click="$refs['gdcd'].open(tableData)">
          选择已有的
        </el-button>
        <el-button size="small" type="danger" :disabled="disabled" @click="removeItem(item,'array')">
          清空
        </el-button>
      </template>
    </avue-crud>

    <get-data-common-dialog title="选择状态"
                            ref="gdcd"
                            id-key="id"
                            :limit="1"
                            :isPage="false"
                            :column="column"
                            url="/api/state-api/life-cycle/workingListOfEnable"
                            @addToList="addToList">
    </get-data-common-dialog>
  </div>
</template>
<script>
import GetDataCommonDialog from "@/views/DMDComponent/getDataCommonDialog.vue";
import index from "../index"
import * as entity from "@/api/modulManage/entity";
import * as relation from "@/api/modulManage/relation";

export default {
  name: "stateRules",
  components: {GetDataCommonDialog},
  mixins: [index],
  inject: ["temp"],
  props: {
    option: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      loading: false,
      tableData: [],
      column: [
        {
          label: "英文名称",
          prop: "nameEn",
          overHidden: true,
        },
        {
          label: '中文名称',
          prop: 'nameCn',
          overHidden: true,
        },
        {
          label: "英文描述",
          prop: "describeEn",
          overHidden: true,
        },
        {
          label: "中文描述",
          prop: "describeCn",
          overHidden: true,
        },
        {
          label: "初始状态",
          prop: "initialStateId",
          dicUrl: "/api/state-api/life-cycle-state/list",
          props: {
            label: "stateName",
            value: "id"
          }
        },
        {
          label: "状态列表",
          prop: "stateList",
          overHidden: true,
        },
      ]
    }
  },
  computed: {
    api() {
      if (this.temp === "1") {
        return entity
      }
      return relation;
    }
  },
  methods: {
    validate() {
      if (this.tableData.length === 0) {
        return {state: false, msg: "状态规则配置"}
      }
      return {state: true}
    },

    addToList(v) {
      let obj = v[0];
      Object.assign(obj, {
        type: this.temp,
        functionName: obj.name,
        interfaceName: "lifecyclable",
        functionType: 3
      })
      this.tableData = [obj];

      this.addToSettingList(obj);

      this.$refs['gdcd'].close();
    }
  },
  watch: {
    show: {
      handler(v) {
        if (v) {
          this.api.getLifeCon({oid: this.formDataAll.oid, type: this.temp}).then(res => {
            if (res.data.data) {
              this.addToList([this.deepCopy(res.data.data)]);
            }
          })
        }
      },
      deep: true,
      immediate: true
    }
  }
}
</script>
